package com.etoak.controller;

import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Ingredients;
import com.etoak.service.IngredientsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author etoak
 * @since 2025-06-30
 */
@RestController
@RequestMapping("/ingredients")
public class IngredientsController {

    private static final Logger log = LoggerFactory.getLogger(IngredientsController.class);

    @Autowired
    IngredientsService ingredientsService;


    @PostMapping
    public ResultVO<Object> add(@Validated @RequestBody Ingredients ingredients){
        ingredientsService.add(ingredients);
        return ResultVO.success();
    }

    @GetMapping("/list")
    public ResultVO<PageVO<Ingredients>> list(
            @RequestParam(required = false,defaultValue = "1") int pageNumber,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            Ingredients ingredients){
        PageVO<Ingredients> pageVO = ingredientsService.listPage(pageNumber, pageSize, ingredients);
        return ResultVO.success(pageVO);
    }

    @GetMapping("/list2")
    public ResultVO<PageVO<Ingredients>> getlist(
            @RequestParam(required = false,defaultValue = "1") int pageNumber,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            Ingredients ingredients){
        PageVO<Ingredients> pageVO = ingredientsService.listPage2(pageNumber,pageSize,ingredients);
        return ResultVO.success(pageVO);
    }

    @PutMapping("/{id}")
    public ResultVO<Object> update(@PathVariable int id,@RequestBody Ingredients ingredients){
        log.info("Updating ingredient with ID: {}", id);
        log.debug("Request data: {}", ingredients);
        ingredientsService.update(id,ingredients);
        return ResultVO.success();
    }

    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id){
        ingredientsService.delete(id);
        return ResultVO.success();
    }

    @GetMapping("/listByType")
    public ResultVO<Object> listByType(@RequestParam String type){
        Ingredients ingredients = new Ingredients();
        ingredients.setType(type);
        List<Ingredients> ingredientsList = ingredientsService.getList(ingredients);
        return ResultVO.success(ingredientsList);
    }

    @GetMapping("/list4")
    public ResultVO<PageVO<Ingredients>> list3(
            @RequestParam(required = false,defaultValue = "1")int pageNumber,
            @RequestParam(required = false,defaultValue = "10")int pageSize,
            Ingredients ingredients){
        PageVO<Ingredients> pageVO = ingredientsService.listPage3(pageNumber, pageSize, ingredients);
        return ResultVO.success(pageVO);
    }
}

